import os
import subprocess

import allure

from utils.LogUtil import logger_init
logger = logger_init("allure")
from config.config import get_path,base_path

def get_allure_severity(case_severity):
    '''
    阻塞 = 'blocker'
    严重 = 'critical'
    一般 = 'normal'
    次要 = 'minor'
    建议 = 'trivial'
    :param case_severity:
    :return:
    '''
    if case_severity=='阻塞':
        return 'blocker'
    elif case_severity =='严重':
        return 'critical'
    elif case_severity =='一般':
        return 'normal'
    elif case_severity =='次要':
        return 'minor'
    elif  case_severity =='建议':
        return 'trivial'
    else:
        raise Exception('没有这个用例等级，请检查是否书写错误')

def gen_allure_report(report_org,html_report):
    logger.info("生成allure测试报告:%s" % html_report)
    try:
        category_json = os.path.join(base_path,'reports',"categories.json")
        cp_cmd = "cp %s %s" % (category_json,report_org)
        subprocess.call(cp_cmd,shell=True)
        cmd = "allure generate %s -o %s --clean" % (report_org,html_report)
        subprocess.call(cmd,shell=True)
    except Exception as e:
        logger.error("生成测试报告失败，错误信息: %s" % e)
        raise
def allure_info(testcase):
    allure.dynamic.feature(testcase['模块'])
    allure.dynamic.title("{}:{}:{}".format(testcase['用例编号'],testcase.get("子模块",""),testcase['测试点']))
    allure.dynamic.description(
        "<b>输入参数</b>:{};<br/>\
        <b>期望alert</b>:{};<br/>\
        <b>期望toast</b>:{};<br/>\
        <b>期望url</b>:{};<br/>\
        <b>期望welcome</b>:{}<br/>".format(
            testcase.get('输入参数',""),
            testcase.get('期望alert',""),
            testcase.get('期望toast',""),
            testcase.get('期望url',""),
            testcase.get('期望welcome',"")))
    allure.dynamic.severity(get_allure_severity(testcase['严重等级']))